package com.health.platform.provider.publichealth.service;

import com.health.platform.common.core.entity.CommonQueryEntity;
import com.health.platform.common.restful.AppResponse;
import com.health.platform.provider.publichealth.dao.PublicHealthProvinceDao;
import com.health.platform.common.core.entity.EchartsEntity;
import com.health.platform.provider.publichealth.entity.VaccinEntity;
import com.health.platform.common.utils.MyUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>公共卫生-省厅</p>
 * <p>创建日期：2021-02-25</p>
 *
 * @author wangyanlei
 */
@Service
public class PublicHealthProvinceService {

    @Resource
    private PublicHealthProvinceDao publicHealthProvinceDao;

    /**
     * 获取中间部分汇总信息
     * 1.获取常住人口数
     * 2.获取建档数
     * 3.重点人群建档数
     * 4.家医签约人数
     * 5.重点人群家医签约人数
     * 6. 高血压管理率+高血压规范率
     * 7. 糖尿病管理率+糖尿病规范率
     * 10. 建档率
     * 11. 家医签约率
     * 12. 重点人群签约率
     * 13. 基本公卫金费标准
     * 14. 截止当年实际到位总资金
     *
     * @param publicHealth 参数
     * @return 结果
     */
    public AppResponse getPersonTotal(CommonQueryEntity publicHealth) {
        Map<String, Object> result = new HashMap<>(14);
        //  ——————————————————————1.获取常住人口数 ——————————————————————
        // 页面选择的日期
        String[] oftenPeople = new String[]{"ZB_TYPE2"};
        publicHealth.setTargetTypeArr(oftenPeople);
        Integer thisYearPeopleTotal = publicHealthProvinceDao.getTargetZBTable(publicHealth);
        thisYearPeopleTotal = thisYearPeopleTotal == null ? 0 : thisYearPeopleTotal;
        // 去年同期
        publicHealth.setCompare(true);
        Integer lastYearPeopleTotal = publicHealthProvinceDao.getTargetZBTable(publicHealth);
        lastYearPeopleTotal = lastYearPeopleTotal == null ? 0 : lastYearPeopleTotal;
        // 计算同比
        String proportion = MyUtils.getProportion(thisYearPeopleTotal, lastYearPeopleTotal);
        result.put("thisYearTotal", thisYearPeopleTotal);
        result.put("peopleProportion", proportion);
        // ——————————————————————2.获取建档数 ——————————————————————
        String[] targetType = new String[]{"GW_TYPE1"};
        // 页面选择的日期
        publicHealth.setTargetTypeArr(targetType);
        publicHealth.setCompare(false);
        Integer thisYearFile = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        thisYearFile = thisYearFile == null ? 0 : thisYearFile;
        // 去年同期
        publicHealth.setCompare(true);
        Integer lastYearFile = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        lastYearFile = lastYearFile == null ? 0 : lastYearFile;
        // 计算同比
        String fileProportion = MyUtils.getProportion(thisYearFile, lastYearFile);
        result.put("thisYearFile", thisYearFile);
        result.put("fileProportion", fileProportion);
        //  ——————————————————————3.重点人群建档数 ——————————————————————
        String[] targetFocusType = new String[]{"GW_TYPE2", "FY_TYPE4", "FY_TYPE13", "GW_TYPE4", "GW_TYPE8", "GW_TYPE12"};
        // 页面选择的日期
        publicHealth.setTargetTypeArr(targetFocusType);
        publicHealth.setCompare(false);
        Integer thisYearFocusFile = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        thisYearFocusFile = thisYearFocusFile == null ? 0 : thisYearFocusFile;
        // 去年同期
        publicHealth.setCompare(true);
        Integer lastYearFocusFile = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        lastYearFocusFile = lastYearFocusFile == null ? 0 : lastYearFocusFile;
        // 计算同比
        String focusFileProportion = MyUtils.getProportion(thisYearFocusFile, lastYearFocusFile);
        result.put("thisYearFocusFile", thisYearFocusFile);
        result.put("focusFileProportion", focusFileProportion);
        //——————————————————————4.家医签约人数——————————————————————
        String[] familyDoctor = new String[]{"GW_TYPE17"};
        // 页面选择的日期
        publicHealth.setTargetTypeArr(familyDoctor);
        publicHealth.setCompare(false);
        Integer thisYearFamilyDoctor = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        thisYearFamilyDoctor = thisYearFamilyDoctor == null ? 0 : thisYearFamilyDoctor;
        // 去年同期
        publicHealth.setCompare(true);
        Integer lastYearFamilyDoctor = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        lastYearFamilyDoctor = lastYearFamilyDoctor == null ? 0 : lastYearFamilyDoctor;
        // 计算同比
        String familyDoctorProportion = MyUtils.getProportion(thisYearFamilyDoctor, lastYearFamilyDoctor);
        result.put("thisYearFamilyDoctor", thisYearFamilyDoctor);
        result.put("familyDoctorProportion", familyDoctorProportion);
        //——————————————————————5.重点人群家医签约人数——————————————————————
        String[] focusFamilyDoctor = new String[]{"GW_TYPE18", "GW_TYPE19", "GW_TYPE20", "GW_TYPE21", "GW_TYPE22", "GW_TYPE23"};
        // 页面选择的日期
        publicHealth.setTargetTypeArr(focusFamilyDoctor);
        publicHealth.setCompare(false);
        Integer thisYearFocusFamilyDoctor = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        thisYearFocusFamilyDoctor = thisYearFocusFamilyDoctor == null ? 0 : thisYearFocusFamilyDoctor;
        // 去年同期
        publicHealth.setCompare(true);
        Integer lastYearFocusFamilyDoctor = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        lastYearFocusFamilyDoctor = lastYearFocusFamilyDoctor == null ? 0 : lastYearFocusFamilyDoctor;
        // 计算同比
        String focusFamilyDoctorProportion = MyUtils.getProportion(thisYearFocusFamilyDoctor, lastYearFocusFamilyDoctor);
        result.put("thisYearFocusFamilyDoctor", thisYearFocusFamilyDoctor);
        result.put("focusFamilyDoctorProportion", focusFamilyDoctorProportion);

        //——————————————————————6. 高血压管理率+高血压规范率——————————————————————
        // 获取高血压管理人数
        String[] hypertensionManage = new String[]{"GW_TYPE4"};
        publicHealth.setTargetTypeArr(hypertensionManage);
        publicHealth.setCompare(false);
        Integer hypertensionManageTotal = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        hypertensionManageTotal = hypertensionManageTotal == null ? 0 : hypertensionManageTotal;
        // 获取高血压患者数
        String[] hypertension = new String[]{"GW_TYPE5"};
        publicHealth.setTargetTypeArr(hypertension);
        Integer hypertensionTotal = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        hypertensionTotal = hypertensionTotal == null ? 0 : hypertensionTotal;
        // 计算高血压管理率
        String hypertensionRate = MyUtils.getRate(hypertensionManageTotal, hypertensionTotal);
        result.put("hypertensionRate", hypertensionRate);
        // 计算高血压规范率
        String[] hypertensionRule = new String[]{"GW_TYPE6"};
        publicHealth.setTargetTypeArr(hypertensionRule);
        Integer hypertensionRuleTotal = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        hypertensionRuleTotal = hypertensionRuleTotal == null ? 0 : hypertensionRuleTotal;
        String hypertensionRuleRate = MyUtils.getRate(hypertensionRuleTotal, hypertensionManageTotal);
        result.put("hypertensionRuleRate", hypertensionRuleRate);
        //——————————————————————7. 糖尿病管理率+糖尿病规范率——————————————————————
        String[] diabetesManage = new String[]{"GW_TYPE8"};
        // 获取糖尿病管理人数
        publicHealth.setTargetTypeArr(diabetesManage);
        Integer diabetesManageTotal = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        diabetesManageTotal = diabetesManageTotal == null ? 0 : diabetesManageTotal;
        // 获取糖尿病患者人数
        String[] diabetes = new String[]{"GW_TYPE9"};
        publicHealth.setTargetTypeArr(diabetes);
        Integer diabetesTotal = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        diabetesTotal = diabetesTotal == null ? 0 : diabetesTotal;
        // 计算糖尿病管理率
        String diabetesManageRate = MyUtils.getRate(diabetesManageTotal, diabetesTotal);
        result.put("diabetesManageRate", diabetesManageRate);
        // 获取糖尿病规范管理数
        String[] diabetesRule = new String[]{"GW_TYPE10"};
        publicHealth.setTargetTypeArr(diabetesRule);
        Integer diabetesRuleTotal = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        diabetesRuleTotal = diabetesRuleTotal == null ? 0 : diabetesRuleTotal;
        // 计算糖尿病规范率
        String diabetesRuleRate = MyUtils.getRate(diabetesRuleTotal, diabetesManageTotal);
        result.put("diabetesRuleRate", diabetesRuleRate);

        //——————————————————————10. 建档率——————————————————————
        // 建档数
        publicHealth.setTargetTypeArr(targetType);
        Integer fileTotal = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        fileTotal = fileTotal == null ? 0 : fileTotal;
        // 常住人口数
        publicHealth.setTargetTypeArr(oftenPeople);
        Integer peopleTotal = publicHealthProvinceDao.getTargetZBTable(publicHealth);
        peopleTotal = peopleTotal == null ? 0 : peopleTotal;
        // 建档率
        String fileTotalRate = MyUtils.getRate(fileTotal, peopleTotal);
        result.put("fileTotalRate", fileTotalRate);
        //——————————————————————11. 家医签约率——————————————————————
        // 家医签约人数
        publicHealth.setTargetTypeArr(familyDoctor);
        Integer familyDoctorTotal = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        familyDoctorTotal = familyDoctorTotal == null ? 0 : familyDoctorTotal;
        // 家医签约率
        String familyDoctorRate = MyUtils.getRate(familyDoctorTotal, peopleTotal);
        result.put("familyDoctorRate", familyDoctorRate);
        //——————————————————————12. 重点人群签约率——————————————————————
        // 重点人群签约数
        publicHealth.setTargetTypeArr(focusFamilyDoctor);
        Integer focusFamilyDoctorTotal = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        focusFamilyDoctorTotal = focusFamilyDoctorTotal == null ? 0 : focusFamilyDoctorTotal;
        // ZB_TABLE表获取数据 老年人口数 儿童人口数
        String[] oldAndChildren = new String[]{"ZB_TYPE3", "ZB_TYPE5"};
        publicHealth.setTargetTypeArr(oldAndChildren);
        Integer oldAndChildrenTotal = publicHealthProvinceDao.getTargetZBTable(publicHealth);
        oldAndChildrenTotal = oldAndChildrenTotal == null ? 0 : oldAndChildrenTotal;
        // TARGET_TABLE1表获取数据  产妇 糖尿病 重性精神病患者数 高血压患者数
        String[] focusPeople = new String[]{"FY_TYPE3", "GW_TYPE9", "GW_TYPE13", "GW_TYPE5"};
        publicHealth.setTargetTypeArr(focusPeople);
        Integer focusPeopleTotal = publicHealthProvinceDao.getTargetGWTable(publicHealth);
        focusPeopleTotal = focusPeopleTotal == null ? 0 : focusPeopleTotal;
        // 重点人群总数
        Integer focusAllTotal = oldAndChildrenTotal + focusPeopleTotal;
        // 重点签约率
        String focusPeopleRate = MyUtils.getRate(focusFamilyDoctorTotal, focusAllTotal);
        result.put("focusPeopleRate", focusPeopleRate);
        //——————————————————————13. 基本公卫金费标准——————————————————————
        publicHealth.setTargetTypeArr(new String[]{"ZB_TYPE10"});
        Integer moneyStandard = publicHealthProvinceDao.getTargetZBTable(publicHealth);
        moneyStandard = moneyStandard == null ? 0 : moneyStandard;
        result.put("moneyStandard", moneyStandard);
        //——————————————————————14. 截止当年实际到位总资金——————————————————————
        publicHealth.setTargetTypeArr(new String[]{"ZB_TYPE11"});
        Integer moneyActual = publicHealthProvinceDao.getTargetZBTable(publicHealth);
        moneyActual = moneyActual == null ? 0 : moneyActual;
        result.put("moneyActual", moneyActual);
        return AppResponse.success("查询成功", result);
    }

    /**
     * 获取中间部分汇总信息[当前年份最近五年数据]
     * 8.妇幼保健
     * 9.血压血糖控制率
     * 15.活产胎次情况
     * 16.老年人健康体检率
     * 17.出生与死亡人数对比
     *
     * @param publicHealth 参数
     * @return 结果
     */
    public AppResponse getEchartsGWByYear(CommonQueryEntity publicHealth) {
        Map<String, Object> result = new HashMap<>(14);
        //——————————————————————8.妇幼保健——————————————————————
        // 早孕建册数
        publicHealth.setTargetType("FY_TYPE1");
        List<EchartsEntity> earlyPregnancyMap = publicHealthProvinceDao.getEchartsGWByYear(publicHealth);
        earlyPregnancyMap = MyUtils.getEchartsDataByYear(earlyPregnancyMap,publicHealth.getRecentYearArr());
        result.put("earlyPregnancyTotal", earlyPregnancyMap);
        // 活产数
        publicHealth.setTargetType("FY_TYPE10");
        List<EchartsEntity> birthMap = publicHealthProvinceDao.getEchartsGWByYear(publicHealth);
        // 按年份拆分 求率
        List<EchartsEntity> earlyPregnancyRate = MyUtils.getEchartsDataByListByYear(earlyPregnancyMap, birthMap,publicHealth.getRecentYearArr());
        result.put("earlyPregnancyRate", earlyPregnancyRate);
        // 新生儿访视数
        publicHealth.setTargetType("FY_TYPE15");
        List<EchartsEntity> newBabyMap = publicHealthProvinceDao.getEchartsGWByYear(publicHealth);
        newBabyMap = MyUtils.getEchartsDataByYear(newBabyMap,publicHealth.getRecentYearArr());
        result.put("newBabyMap", newBabyMap);
        // 新生儿访视率
        List<EchartsEntity> newBabyRate = MyUtils.getEchartsDataByListByYear(newBabyMap, birthMap,publicHealth.getRecentYearArr());
        result.put("newBabyRate", newBabyRate);
        //——————————————————————9.血压控制率、血糖控制率——————————————————————
        // 血压达标人数
        publicHealth.setTargetType("FY_TYPE7");
        List<EchartsEntity> bloodPressureTotal = publicHealthProvinceDao.getEchartsGWByYear(publicHealth);
        // 已管理的高血压患者数
        publicHealth.setTargetType("FY_TYPE4");
        List<EchartsEntity> hypertensionTotal = publicHealthProvinceDao.getEchartsGWByYear(publicHealth);
        // 血压控制率
        List<EchartsEntity> bloodPressureControlRate = MyUtils.getEchartsDataByListByYear(bloodPressureTotal, hypertensionTotal,publicHealth.getRecentYearArr());
        result.put("bloodPressureControlRate", bloodPressureControlRate);
        // 血糖控制人数
        publicHealth.setTargetType("FY_TYPE11");
        List<EchartsEntity> bloodSugarTotal = publicHealthProvinceDao.getEchartsGWByYear(publicHealth);
        publicHealth.setTargetType("FY_TYPE8");
        List<EchartsEntity> diabetesTotal = publicHealthProvinceDao.getEchartsGWByYear(publicHealth);
        // 血糖控制率
        List<EchartsEntity> bloodSugarControlRate = MyUtils.getEchartsDataByListByYear(bloodSugarTotal, diabetesTotal,publicHealth.getRecentYearArr());
        result.put("bloodSugarControlRate", bloodSugarControlRate);
        //——————————————————————15. 活产胎次情况——————————————————————
        publicHealth.setTargetType("FY_TYPE10");

        //一胎
        publicHealth.setSpareCode1("1");
        List<EchartsEntity> aliveEveryYear = publicHealthProvinceDao.getEchartsGWByYear(publicHealth);
        aliveEveryYear = MyUtils.getEchartsDataByYear(aliveEveryYear,publicHealth.getRecentYearArr());
        result.put("aliveEveryYear", aliveEveryYear);
        //二胎
        publicHealth.setSpareCode1("2");
        List<EchartsEntity> aliveEveryYearTwo = publicHealthProvinceDao.getEchartsGWByYear(publicHealth);
        aliveEveryYearTwo = MyUtils.getEchartsDataByYear(aliveEveryYearTwo,publicHealth.getRecentYearArr());
        result.put("aliveEveryYearTwo", aliveEveryYearTwo);
        //三胎
        publicHealth.setSpareCode1("3");
        List<EchartsEntity> aliveEveryYearThree = publicHealthProvinceDao.getEchartsGWByYear(publicHealth);
        aliveEveryYearThree = MyUtils.getEchartsDataByYear(aliveEveryYearThree,publicHealth.getRecentYearArr());
        result.put("aliveEveryYearThree", aliveEveryYearThree);
        publicHealth.setSpareCode1(null);
        //——————————————————————16. 老年人健康体检率——————————————————————
        // 老年人体检数
        publicHealth.setTargetType("GW_TYPE3");
        List<EchartsEntity> oldPeopleCheck = publicHealthProvinceDao.getEchartsGWByYear(publicHealth);
        // 常住老年人总数
        publicHealth.setTargetType("ZB_TYPE3");
        List<EchartsEntity> oldPeopleTotal = publicHealthProvinceDao.getEchartsZBByYear(publicHealth);
        // 老年人健康体检率
        List<EchartsEntity> oldPeopleCheckRate = MyUtils.getEchartsDataByListByYear(oldPeopleCheck, oldPeopleTotal,publicHealth.getRecentYearArr());
        result.put("oldPeopleCheckRate", oldPeopleCheckRate);
        //——————————————————————17. 出生与死亡人数对比——————————————————————
        // 出生人数
        publicHealth.setTargetType("FY_TYPE10");
        List<EchartsEntity> bornEveryYear = publicHealthProvinceDao.getEchartsGWByYear(publicHealth);
        bornEveryYear = MyUtils.getEchartsDataByYear(bornEveryYear,publicHealth.getRecentYearArr());
        result.put("bornEveryYear", bornEveryYear);
        // 死亡人数
        publicHealth.setTargetType("FY_TYPE24");
        List<EchartsEntity> deathEveryYear = publicHealthProvinceDao.getEchartsGWByYear(publicHealth);
        deathEveryYear = MyUtils.getEchartsDataByYear(deathEveryYear,publicHealth.getRecentYearArr());
        result.put("deathEveryYear", deathEveryYear);
        return AppResponse.success("查询成功", result);
    }

    /**
     * 获取排名的信息
     * 18.低收入人口疾病排名
     * 19.疫苗接种人次排名
     *
     * @param publicHealth 参数
     * @return 结果
     */
    public AppResponse getSort(CommonQueryEntity publicHealth) {
        Map<String, Object> result = new HashMap<>(14);
        // 低收入人口疾病排名
        List<EchartsEntity> diseaseSort = publicHealthProvinceDao.getDiseaseSort(publicHealth);
        result.put("diseaseSort", diseaseSort);
        // 疫苗接种人次排名
        List<VaccinEntity> vaccineSort = publicHealthProvinceDao.getVaccineSort(publicHealth);
        result.put("vaccineSort", vaccineSort);
        return AppResponse.success("查询成功", result);
    }
}
